package com.lut.teacher.teacherservice.service.impl;

import com.lut.teacher.teacherservice.dao.TeacherTitleDao;
import com.lut.teacher.teacherservice.entity.*;
import com.lut.teacher.teacherservice.entity.vo.TeacherInfoVo;
import com.lut.teacher.teacherservice.service.*;
import com.lut.teacher.teacherservice.utils.PageUtils;
import com.lut.teacher.teacherservice.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lut.teacher.teacherservice.dao.TeacherInfoDao;
import org.springframework.util.StringUtils;


@Service("teacherInfoService")
public class TeacherInfoServiceImpl extends ServiceImpl<TeacherInfoDao, TeacherInfoEntity> implements TeacherInfoService {
     @Autowired
     private TeacherTitleService teacherTitleService;
     @Autowired
     private TeacherProjectService teacherProjectService;
     @Autowired
    private  TeacherPaperService teacherPaperService;
     @Autowired
     private TeacherEductionService teacherEductionService;
     @Autowired
     private  TeacherCourseService teacherCourseService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TeacherInfoEntity> wrapper = new QueryWrapper<>();
        String key= (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.like("name",key);
        }


        IPage<TeacherInfoEntity> page = this.page(
                new Query<TeacherInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<TeacherInfoVo> queryteacher() {
        List<TeacherInfoEntity> teacherInfoEntities = this.baseMapper.selectList(null);

        List<TeacherInfoVo> collect = teacherInfoEntities.stream().map(iteam -> {
            TeacherInfoVo teacherInfoVo = new TeacherInfoVo();
            BeanUtils.copyProperties(iteam, teacherInfoVo);
            return teacherInfoVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void removeAll(List<String> asList) {
        // 1.删除教师的基本信息
        this.baseMapper.deleteBatchIds(asList);

        for(String id:asList){
            QueryWrapper<TeacherCourseEntity> wrapper=new QueryWrapper<TeacherCourseEntity>().eq("teacher_id",id);
            teacherCourseService.remove(wrapper);
            QueryWrapper<TeacherProjectEntity> wrapper1=new QueryWrapper<TeacherProjectEntity>().eq("teacher_id",id);
            teacherProjectService.remove(wrapper1);
            QueryWrapper<TeacherPaperEntity> wrapper2=new QueryWrapper<TeacherPaperEntity>().eq("teacher_id",id);
            teacherPaperService.remove(wrapper2);
            QueryWrapper<TeacherTitleEntity> wrapper3=new QueryWrapper<TeacherTitleEntity>().eq("teacher_id",id);
            teacherTitleService.remove(wrapper3);
            QueryWrapper<TeacherEductionEntity> wrapper4=new QueryWrapper<TeacherEductionEntity>().eq("teacher_id",id);
            teacherEductionService.remove(wrapper4);
        }


    }

    @Override
    public DetailVo getAllInfoById(String id) {
        DetailVo detailVo=new DetailVo();
        TeacherInfoEntity teacherInfoEntity = this.baseMapper.selectById(id);
        BeanUtils.copyProperties(teacherInfoEntity,detailVo);
        QueryWrapper<TeacherCourseEntity> wrapper=new QueryWrapper<TeacherCourseEntity>().eq("teacher_id",id);
        List<TeacherCourseEntity> list = teacherCourseService.list(wrapper);
        detailVo.setCourseEntityList(list);
        QueryWrapper<TeacherProjectEntity> wrapper1=new QueryWrapper<TeacherProjectEntity>().eq("teacher_id",id);
        List<TeacherProjectEntity> list1 = teacherProjectService.list(wrapper1);
        detailVo.setProjectEntities(list1);
        QueryWrapper<TeacherPaperEntity> wrapper2=new QueryWrapper<TeacherPaperEntity>().eq("teacher_id",id);
        List<TeacherPaperEntity> list2 = teacherPaperService.list(wrapper2);
        detailVo.setPaperEntities(list2);
        QueryWrapper<TeacherTitleEntity> wrapper3=new QueryWrapper<TeacherTitleEntity>().eq("teacher_id",id);
        List<TeacherTitleEntity> list3 = teacherTitleService.list(wrapper3);
        detailVo.setTeacherTitleEntities(list3);
        QueryWrapper<TeacherEductionEntity> wrapper4=new QueryWrapper<TeacherEductionEntity>().eq("teacher_id",id);
        List<TeacherEductionEntity> list4 = teacherEductionService.list(wrapper4);
        detailVo.setEductionEntities(list4);


        return detailVo;
    }

}